{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 求解一元二次方程\n",
    "import math\n",
    "a = float(input(\"a = \"))\n",
    "b = float(input(\"b = \"))\n",
    "c = float(input(\"c = \"))\n",
    "\n",
    "delta = pow(b, 2) - 4 * a * c\n",
    "if delta > 0:         \n",
    "    x1 = (- b + math.sqrt(delta)) / (2 * a)\n",
    "    x2 = (- b - math.sqrt(delta)) / (2 * a)\n",
    "print(\"x1 = {:.2f}\".format(x1))\n",
    "print(\"x2 = {:.2f}\".format(x2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1~1000间的完数共有0个\n"
     ]
    }
   ],
   "source": [
    "# 3. 判断完数（一个数恰好等于它的所有因子之和）\n",
    "# 例如，6的因子为1、2、3，而6=1+2+3，因此6是完数\n",
    "# 找出1000之内的所有完数，并输出该完数，统计1000之内一共有多少个完数\n",
    "\n",
    "import math\n",
    "\n",
    "def is_perfect_number(num):\n",
    "    sum_factors = 1\n",
    "    for i in range(2, num):\n",
    "        if num % i == 0:\n",
    "            sum_factors += i\n",
    "    return (sum_factors == num)\n",
    "\n",
    "count = 0\n",
    "for i in range(2, 1000):\n",
    "    if is_perfect_number(i):\n",
    "        print(i)\n",
    "        count += 1\n",
    "\n",
    "print(f\"1~1000间的完数共有{count}个\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 求阶乘之和\n",
    "# 具体要求：\n",
    "# 请在下面三处划线处填入适当的表达式，完整程序，实现程序功能。\n",
    "n = int(input( ))\n",
    "if n < 0:\n",
    "    print(\"n输入错误！\")\n",
    "    exit(0)\n",
    "s, t = 0, 1\n",
    "for i in range(1, n + 1):\n",
    "        t *= i\n",
    "        s =s + t\n",
    "if n == 0:\n",
    "    print(\"阶乘和为：\", 1)\n",
    "else:\n",
    "    print(\"阶乘和为：\", s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 请修改下面的代码，实现正确程序\n",
    "\n",
    "x = input()\n",
    "if x == \"quit\":\n",
    "    print(\"程序结束\")\n",
    "    exit(0)\n",
    "else:\n",
    "    maxnum = minnum = int(x)\n",
    "\n",
    "s = int(x)\n",
    "n = 1\n",
    "\n",
    "while True:\n",
    "    x = input()\n",
    "    if x == \"quit\":\n",
    "        print(\"程序结束\")\n",
    "        break\n",
    "    x = int(x)\n",
    "    if x > maxnum:\n",
    "        maxnum = x\n",
    "    if x < minnum:\n",
    "        minnum = x\n",
    "    s = s + x\n",
    "    n = n + 1\n",
    "print(\"最高气温为：{},最低气温为：{}\".format(maxnum, minnum))\n",
    "print(\"平均气温为：{:.1f} \".format(s / n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6. 回文数\n",
    "\n",
    "str = input()\n",
    "if len(str) != 5:\n",
    "    print(\"输入长度错误！\")\n",
    "if str.isdigit() == False:\n",
    "    print(\"输入的不是自然数！\")\n",
    "elif str == str[::-1]:\n",
    "    print(str,\"是回文数\")\n",
    "else:\n",
    "    print(str,\"不是回文数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 7. 输入一段文本，输出该文本中出现频率最高的字符。如果频率最高的字母有多个，输出先出现的字母。\n",
    "\n",
    "str = input()\n",
    "max_char = ''\n",
    "max_count = 0\n",
    "\n",
    "for char in str:\n",
    "    count = str.count(char)\n",
    "    if count > max_count: # 采用>而不是≥，这样出现相同频次的字符，不会替换\n",
    "        max_count = count\n",
    "        max_char = char\n",
    "print(max_char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(2, 4), (3, 6), (4, 2), (7, 3), (10, 1), (11, 2)]\n"
     ]
    }
   ],
   "source": [
    "# 9. 统计不同长度的单词个数\n",
    "# 随即输入一行字符串，统计其中不同长度的单词出现次数，输出单词长度和对应单词个数，以列表形式输出。\n",
    "# 按单词长度从小到大排序\n",
    "# 标点符号不能计入单词长度，在统计前可以将所有的标点符号置换为空字符。\n",
    "# 输出格式为：[(单词长度，单词个数),(单词长度，单词),......]\n",
    "\n",
    "from collections import Counter\n",
    "str = input()\n",
    "\n",
    "# str = \"Welcome! Are you completely new to programming? If not then we presume you will be looking for information.\"\n",
    "\n",
    "# 置换标点符号并分割单词\n",
    "for char in str:\n",
    "    if (not char.isalpha()) and (char != \" \"):\n",
    "        str = str.replace(char, \" \")\n",
    "\n",
    "# print(str)\n",
    "# str_new = str.replace(\"!\", \" \").replace(\"?\",\" \").replace(\",\",\" \").replace(\".\",\" \")\n",
    "words = []\n",
    "words = str.split()\n",
    "# print(words)\n",
    "\n",
    "# 统计str中每个单词的长度\n",
    "length_words = []\n",
    "for word in words:\n",
    "    length_words.append(len(word))\n",
    "\n",
    "# 统计length_words中不同长度的单词出现次数\n",
    "count = Counter(length_words)\n",
    "\n",
    "# 排序并打印，sorted函数默认升序排列\n",
    "print(sorted(count.items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2, 4], [3, 6], [4, 2], [7, 3], [10, 1], [11, 2]]\n"
     ]
    }
   ],
   "source": [
    "# 9. 统计不同长度的单词个数\n",
    "# 随即输入一行字符串，统计其中不同长度的单词出现次数，输出单词长度和对应单词个数，以列表形式输出。\n",
    "# 按单词长度从小到大排序\n",
    "# 标点符号不能计入单词长度，在统计前可以将所有的标点符号置换为空字符。\n",
    "# 输出格式为：[(单词长度，单词个数),(单词长度，单词),......]\n",
    "\n",
    "# from collections import Counter\n",
    "# str = input()\n",
    "\n",
    "str = \"Welcome! Are you completely new to programming? If not then we presume you will be looking for information.\"\n",
    "\n",
    "# 置换标点符号并分割单词\n",
    "for char in str:\n",
    "    if (not char.isalpha()) and (char != \" \"):\n",
    "        str = str.replace(char, \" \")\n",
    "\n",
    "words = []\n",
    "words = str.split()\n",
    "\n",
    "# 统计str中每个单词的长度\n",
    "length_words = []\n",
    "for word in words:\n",
    "    length_words.append(len(word))\n",
    "\n",
    "# 统计length_words中不同长度的单词出现次数（字典）\n",
    "dic = dict()\n",
    "for l in length_words:\n",
    "    if l in dic.keys():\n",
    "        dic[l] += 1\n",
    "    else:\n",
    "        dic[l] = 1\n",
    "\n",
    "# 排序并打印，sorted函数默认升序排列\n",
    "L = list((k, v) for k, v in sorted(dic.items()))\n",
    "print(L)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
